package com.comment.project.excel;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liupenghui
 * @date 2018/7/12
 * @desc：
 */
public class ReadExcel {

    public static void main(String[] args) {

        InputStream inputStream = null;
//            inputStream = new FileInputStream("C:/Users/Administrator/Desktop/Book1.xlsx");
            inputStream = ReadExcel.class.getClassLoader().getResourceAsStream("Book1.xlsx");

        Map<String, Object> map = new HashMap<>();
        List<Integer> list = readExcelFile(map, inputStream);
        System.out.println();
    }

    public static List<Integer> readExcelFile(Map map, InputStream inputStream) {
        List<Integer> ids = new ArrayList<>();
        Workbook wb = null;
        try {
            wb = new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            System.out.println("解析异常");
        }
        // 得到第一个Sheet
        Sheet sheet = wb.getSheetAt(0);
        // 得到Excel的行数
        int totalRows = sheet.getPhysicalNumberOfRows();
        if (totalRows < 1) {
            System.out.println("表格没有数据");
        }
        if (sheet.getRow(0) != null) {
            System.out.println(sheet.getRow(0).getCell(0).getNumericCellValue());
        }
        for (int r = 1; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                break;
            }
            ids.add((int) row.getCell(0).getNumericCellValue());

        }
        return ids;
    }


}
